package com.quizh.euro.server.dao;

import static com.googlecode.objectify.ObjectifyService.ofy;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.cmd.Query;
import com.quizh.euro.shared.Boleto;

public class BoletoDAO {

	static {
		ObjectifyService.factory().register(Boleto.class);
	}

	public void save(Boleto object) {
		ofy().save().entity(object);
	}

	public List<Boleto> get(String email) {
		List<Boleto> result = ofy().load().type(Boleto.class)
				.filter(Boleto.EMAIL, email).list();
		return result;
	}

	public List<Boleto> search(List<String> num, List<String> stars) {

		List<String> filter = new ArrayList<String>();
		filter.addAll(num);
		for (String e : stars) {
			filter.add(Boleto.STAR + e);
		}
		 Query<Boleto> adf = ofy().load().type(Boleto.class)
				.filter(Boleto.INDEX + " in", filter);
		 System.out.println(adf.toString());
		List<Boleto> result = adf.list();
		return result;
	}

	public Boleto getLast(String email) {
		List<Boleto> result = ofy().load().type(Boleto.class)
				.filter(Boleto.EMAIL, email).limit(1).order('-'+Boleto.DATE).list();
		
		return result.get(0);
	}
}
